import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Article from '../views/Article.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        component: Login
    }, {
        path: '/',
        component: Home,
        children: [
            {
                path: '/article',
                component: Article
            },
        ]
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path == '/login') {
        next()
    }
    const $token = window.sessionStorage.getItem('token')
    if (!$token
        || $token == ''
        || $token == null ||
        $token == undefined) {
        next('/login')
    }
    next()
})

export default router
